var car = require('../../../template/car/innercakecar.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isReady: false,
    goodscount: 0,
    indicatorDots: true,
    autoplay: false,
    dotcolor: 'rgba(0,0,0,0.3)',
    dotactivecolor: '#ffab3a',
    interval: 5000,
    duration: 1000,
    imgurls: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.address){
      var address = JSON.parse(decodeURIComponent(options.address));
      this.setData({
        address: address
      });
    }
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var wtd = res.windowWidth;
        var hgt = wtd * 603 / 750;
        that.setData({
          imgwtd: wtd,
          imghgt: hgt
        });
      },
    });

    if (app.globalData.tempcar != null) {
      var goodslist = app.globalData.tempcar.list1;
      var goodsidlist = app.globalData.tempcar.list2;
      car.init(this, goodslist, goodsidlist);
      app.globalData.tempcar = null;
    }
    else {
      car.init(this);
    }
    this.setData({
      isReady: false
    });
    this.loadGoodsDetail(options.goodsid);
  },
  loadGoodsDetail: function (goodsid) {
    wx.showLoading({
      title: '',
    });
    var url = '/yuanmai_we/mcake/product/getProductDetail/' + goodsid;
    app.loadDataFromServer(url, this.loadGoodsDetailCallBack, {}, 'get', true);
  },
  loadGoodsDetailCallBack: function (res) {
    console.log(res);
    wx.hideLoading();

    var goods = res.data;
    var imgs = [];
    var pre_url = goods.list[0].pic.url;
    var pics = goods.list[0].pic.list;
    for (var i = 0; i < pics.length; i++) {
      var imgurl = pre_url + pics[i].b;
      imgs.push(imgurl);
    }

    var detailImgUrl = goods.details;
    detailImgUrl = detailImgUrl.replace('<img src="', '');
    detailImgUrl = detailImgUrl.replace('" />', '');
    detailImgUrl = detailImgUrl.replace('"/>', '');
    console.log(detailImgUrl);

    var basics = goods.basic.list;
    var mater = goods.mater.list;
    for (var i = 0; i < mater.length; i++) {
      mater[i].img = res.data.mater.url + mater[i].img;
    }
    this.setData({
      goods: goods,
      curspec: 0,
      imgurls: imgs,
      detailImgUrl: detailImgUrl,
      basic: basics,
      mater: mater,
      isReady: true
    });
    this.updateGoodsCount();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('unshow1');
    if (app.globalData.tempcar != null){
      return;
    }
    app.globalData.tempcar = {
      list1: this.data.goodslist,
      list2: this.data.goodsidlist
    };
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('unshow2');
    if (app.globalData.tempcar != null) {
      return;
    }
    app.globalData.tempcar = {
      list1: this.data.goodslist,
      list2: this.data.goodsidlist
    };
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.goods.name,
      desc: this.data.goods.brief,
      path: 'pages/cake/cakedetail/cakedetail?goodsid=' + this.data.goods.id 
    };
  },
  chooseSpec: function (e) {
    var curspec = e.currentTarget.dataset.specindex;
    this.setData({
      curspec: curspec
    });

    this.updateGoodsCount();
  },
  handleDetailCount: function (e) {
    var options = parseInt(e.currentTarget.dataset.options);
    var goodsinfo = this.data.goods;
    var num = 1;
    var specindex = this.data.curspec;

    var small_img = goodsinfo.list[specindex].pic.url + goodsinfo.list[specindex].pic.list[0].s;
    var nameSpec = goodsinfo.name + ' ' + goodsinfo.list[specindex].spec;
    var data = {
      options: options,
      goodsid: goodsinfo.list[specindex].id,
      price: goodsinfo.list[specindex].salePrice,
      sku: goodsinfo.list[specindex].sku,
      spec: goodsinfo.list[specindex].spec,
      size: goodsinfo.list[specindex].size,
      tips: goodsinfo.list[specindex].edible,
      name: nameSpec,
      shortname: goodsinfo.name,
      img: goodsinfo.img,
      icon: goodsinfo.img,
      image: small_img,
      num: num
    };
    
    car.handleCount(data);
    this.updateGoodsCount();
  },
  addCarAction: function () {
    if (this.data.goodscount <= 0) {
      return;
    }
    app.globalData.cake_temp_goods = {
      goods: this.data.goods,
      curspec: this.data.curspec,
      num: this.data.goodscount
    };
    wx.navigateBack({});
  },
  updateGoodsCount:function(){
    var goodslist = this.data.goodslist;
    var goodsidlist = this.data.goodsidlist;
    var curid = this.data.goods.list[this.data.curspec].id;
    var goodsCount;
    if(goodsidlist.indexOf(curid) >= 0){
      goodsCount = goodslist[goodsidlist.indexOf(curid)].num;
    }
    else{
      goodsCount = 0;
    }
    this.setData({
      goodscount: goodsCount
    });
  },
  // -------------------car-----------------
  showCar: car.showCar,
  submitOrder: car.submitOrder,
  closeCar: car.closeCar,
  handleCount:function(e){
    car.handleCount(e);
    this.updateGoodsCount();
  }
})